package com.example.it.looker.utils;

import java.io.InputStream;

/**
 * Created by 鲁迅认识的那只猹 on 9/1/2017 10:41 AM.
 * Emial 1258730808@qq.com
 * Desc:
 */

/**
 * Stream的帮助类,处理流的读取，转换等等
 */
public class StreamUtil {
    /**
     * 将InputStream转换为String字符串
     *
     * @param inputStream 将要被转换的InputStream流
     * @param encoding    转换的编码，可为空
     * @return 转换后的String
     */
    public static String toString(InputStream inputStream, String encoding) {
        try {
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            inputStream.close();
            if (encoding != null)
                return new String(buffer, encoding);
            return new String(buffer);
        } catch (Exception e) {
            e.printStackTrace();
            L.i("StreamUtil-toString:Error");
            return null;
        }
    }
}
